package app.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import app.annotation.RequestPlanAnnotation;
import app.model.Book;
import app.model.QBook;
import app.repository.BookRepository;

@RestController
@RequestMapping("/book")
public class BookController {

	private static Logger logger = LoggerFactory.getLogger(BookController.class);
	
	@Autowired
	private BookRepository bookRepository;

	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> createBook() {
		Book book = new Book("11", "12", "test", 111);

		bookRepository.save(book);
		Map<String, Object> response = new LinkedHashMap<String, Object>();
		response.put("message", "Book created successfully");
		response.put("book", book);
		return response;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/{bookId}")
	public Book getBookDetails(@PathVariable("bookId") String bookId) {
//		return bookRepository.findOne(bookId);
		return null;
	}

	@RequestMapping(method = RequestMethod.PUT, value = "/{bookId}")
	public Map<String, Object> editBook(@PathVariable("bookId") String bookId,
			@RequestBody Map<String, Object> bookMap) {
		Book book = new Book(bookMap.get("name").toString(), bookMap.get("isbn").toString(),
				bookMap.get("author").toString(), Integer.parseInt(bookMap.get("pages").toString()));
		book.setId(bookId);

		Map<String, Object> response = new LinkedHashMap<String, Object>();
		response.put("message", "Book Updated successfully");
		response.put("book", bookRepository.save(book));
		return response;
	}
	
	@RequestPlanAnnotation(title="测试方法",content="测试方法的内容")
	@RequestMapping(method = RequestMethod.GET, value = "/test/112")
	public List<Book> get(){
		logger.info("你好。。。。。");
		QBook book = QBook.book;
//		BooleanExpression booleanExpression = book.id.eq("572854aed37b40e4c5899a77");
//		Iterable<Book> all = bookRepository.findAll(booleanExpression);
		System.out.println("all===>"+bookRepository);
		
//		Book one = bookRepository.findOne("572854aed37b40e4c5899a77");
//		System.out.println("11"+one.toString());
		return new ArrayList<Book>();
	}
}